'''
爬取网址(tao bao)：
https://uland.taobao.com/sem/tbsearch?clk1=bdd3157b0a87d470cb64bb0adb059e35&keyword=%E6%B7%98%E5%AE%9D%E7%99%BB%E5%BD%95%E7%BD%91%E9%A6%96%E9%A1%B5&localImgKey=&page=1&q=%E6%B7%98%E5%AE%9D%E7%99%BB%E5%BD%95%E7%BD%91%E9%A6%96%E9%A1%B5&refpid=mm_26632258_3504122_32538762&tab=all&upsId=bdd3157b0a87d470cb64bb0adb059e35

1.请求sign的封装
    搜索 sign: js生成核心代码 l = c(r.token + "&" + u + "&" + s + "&" + n.data)
2.使用pyexecjs 执行js代码
    js代码来源：在控制台（前提改代码块已打上断点）
    执行：c(r.token + "&" + u + "&" + s + "&" + n.data)
         c
3.将js代码执行结果，拼接给请求params 体，发起请求；

4.改变 page：页数发起请求；

使用到了 网页F12 请求地址复制cUrl("bash") 在该网址生成爬取代码：https://curlconverter.com/
'''
import requests

cookies = {
    't': 'a9df40545c0d2b295c0fc20e90debbd7',
    'cookie2': '136b2be898e8b3029f8e357609df0282',
    '_tb_token_': '5f33ebbbf3e16',
    'cna': 'cH5tH6NOthUBASQJikQuVGQH',
    'thw': 'xx',
    'xlly_s': '1',
    '_samesite_flag_': 'true',
    '3PcFlag': '1726386305719',
    'mtop_partitioned_detect': '1',
    '_m_h5_tk': 'd5c46a8bd8ec10d1a108549664e2b44e_1726401771086',
    '_m_h5_tk_enc': '19e36e983c444dc4cee28fe9a43575c1',
    'tfstk': 'fG4sdD9W9dvs4uINVICEAX7QO5ubC-_P1IGYZSLwMV39ksw4Lrox3G2jc-ynDPua_JMYUJM9uiDwlqNbn-ybi1cxH-ej3lSFz5VioqBrha7zsN6Rc3EXkxFdMbh6ag3Lm5VioBAeHgrLsn95icNtMxCIJjGjHqnxkHCIgjttkAnvpHh-pqHYHAdppbhJXjntDvCIivHxkqhvR9GhfAF19biLdrp59SMBkUdo6cQ4_n8Do0htf4Fsd65URfiseD8gD3zTLSUaSDj6XqVUV-ZYe9-ZBuG7d0r1FFw7IjFjMR_kNvU_GyigYdKtNVMs2PnRuENsDkaI7PBDqWPIB0gUYMXZ0Vw_qYmOx92bOAyY5Dp1LxrzTPnQe98QnmZY72UOpasPNUk7ggtXO0YjOY5COnxcAPZmyajMqtmttXzPO696mchnO4fCOn4rXXcHi61B4_5..',
    'isg': 'BK2taHWGt-po41MGz-FtHc7OvEknCuHc7kgrku-ztsS-ZskYtlm5rLTwUTqAIvmU',
}

headers = {
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'cache-control': 'no-cache',
    # 'cookie': 't=a9df40545c0d2b295c0fc20e90debbd7; cookie2=136b2be898e8b3029f8e357609df0282; _tb_token_=5f33ebbbf3e16; cna=cH5tH6NOthUBASQJikQuVGQH; thw=xx; xlly_s=1; _samesite_flag_=true; 3PcFlag=1726386305719; mtop_partitioned_detect=1; _m_h5_tk=d5c46a8bd8ec10d1a108549664e2b44e_1726401771086; _m_h5_tk_enc=19e36e983c444dc4cee28fe9a43575c1; tfstk=fG4sdD9W9dvs4uINVICEAX7QO5ubC-_P1IGYZSLwMV39ksw4Lrox3G2jc-ynDPua_JMYUJM9uiDwlqNbn-ybi1cxH-ej3lSFz5VioqBrha7zsN6Rc3EXkxFdMbh6ag3Lm5VioBAeHgrLsn95icNtMxCIJjGjHqnxkHCIgjttkAnvpHh-pqHYHAdppbhJXjntDvCIivHxkqhvR9GhfAF19biLdrp59SMBkUdo6cQ4_n8Do0htf4Fsd65URfiseD8gD3zTLSUaSDj6XqVUV-ZYe9-ZBuG7d0r1FFw7IjFjMR_kNvU_GyigYdKtNVMs2PnRuENsDkaI7PBDqWPIB0gUYMXZ0Vw_qYmOx92bOAyY5Dp1LxrzTPnQe98QnmZY72UOpasPNUk7ggtXO0YjOY5COnxcAPZmyajMqtmttXzPO696mchnO4fCOn4rXXcHi61B4_5..; isg=BK2taHWGt-po41MGz-FtHc7OvEknCuHc7kgrku-ztsS-ZskYtlm5rLTwUTqAIvmU',
    'pragma': 'no-cache',
    'referer': 'https://uland.taobao.com/sem/tbsearch?clk1=bdd3157b0a87d470cb64bb0adb059e35&keyword=%E6%B7%98%E5%AE%9D%E7%99%BB%E5%BD%95%E7%BD%91%E9%A6%96%E9%A1%B5&localImgKey=&page=1&q=%E7%94%B7%E8%A3%85&refpid=mm_26632258_3504122_32538762&tab=all&upsId=bdd3157b0a87d470cb64bb0adb059e35',
    'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Microsoft Edge";v="128"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'script',
    'sec-fetch-mode': 'no-cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',
}

params = {
    'jsv': '2.7.2',
    'appKey': '12574478',
    # 't': '1726395020331',
    # 'sign': 'f784c136068e74f80824ba4305d0f251',
    'api': 'mtop.relationrecommend.wirelessrecommend.recommend',
    'v': '2.0',
    'type': 'jsonp',
    'dataType': 'jsonp',
    'callback': 'mtopjsonp11',
    'data': '{"appId":"43356","params":"{\\"device\\":\\"HMA-AL00\\",\\"isBeta\\":\\"false\\",\\"grayHair\\":\\"false\\",\\"from\\":\\"nt_history\\",\\"brand\\":\\"HUAWEI\\",\\"info\\":\\"wifi\\",\\"index\\":\\"4\\",\\"rainbow\\":\\"\\",\\"schemaType\\":\\"auction\\",\\"elderHome\\":\\"false\\",\\"isEnterSrpSearch\\":\\"true\\",\\"newSearch\\":\\"false\\",\\"network\\":\\"wifi\\",\\"subtype\\":\\"\\",\\"hasPreposeFilter\\":\\"false\\",\\"prepositionVersion\\":\\"v2\\",\\"client_os\\":\\"Android\\",\\"gpsEnabled\\":\\"false\\",\\"searchDoorFrom\\":\\"srp\\",\\"debug_rerankNewOpenCard\\":\\"false\\",\\"homePageVersion\\":\\"v7\\",\\"searchElderHomeOpen\\":\\"false\\",\\"search_action\\":\\"initiative\\",\\"sugg\\":\\"_4_1\\",\\"sversion\\":\\"13.6\\",\\"style\\":\\"list\\",\\"ttid\\":\\"600000@taobao_pc_10.7.0\\",\\"needTabs\\":\\"true\\",\\"areaCode\\":\\"CN\\",\\"vm\\":\\"nw\\",\\"countryNum\\":\\"156\\",\\"m\\":\\"pc_sem\\",\\"page\\":3,\\"n\\":48,\\"q\\":\\"%E7%94%B7%E8%A3%85\\",\\"qSource\\":\\"history\\",\\"pageSource\\":\\"\\",\\"tab\\":\\"all\\",\\"pageSize\\":48,\\"totalPage\\":100,\\"totalResults\\":4800,\\"sourceS\\":\\"0\\",\\"sort\\":\\"_coefp\\",\\"bcoffset\\":\\"\\",\\"ntoffset\\":\\"\\",\\"filterTag\\":\\"\\",\\"service\\":\\"\\",\\"prop\\":\\"\\",\\"loc\\":\\"\\",\\"start_price\\":null,\\"end_price\\":null,\\"startPrice\\":null,\\"endPrice\\":null,\\"itemIds\\":null,\\"p4pIds\\":null,\\"categoryp\\":\\"\\",\\"myCNA\\":\\"cH5tH6NOthUBASQJikQuVGQH\\",\\"clk1\\":\\"bdd3157b0a87d470cb64bb0adb059e35\\",\\"refpid\\":\\"mm_26632258_3504122_32538762\\"}"}',
}


# 获取data值
data2 = params['data']
# print(data2)
# print(params)


import execjs
# 可以编译一个js文件
with open('./MouBaoNanZhuangCode.js', 'r', encoding='utf-8') as f:
    js_code2 = f.read()

ctx2 = execjs.compile(js_code2)
result = ctx2.call("test", data2)
# print(result) # {'time': 1726393177444, 'sign': '052c50db50116fdc048980ccba6bd21d'}

# 将js执行结果 result 添加到 params中
params['t'] = result['time']
params['sign'] = result['sign']
print(params)



response = requests.get(
    'https://h5api.m.taobao.com/h5/mtop.relationrecommend.wirelessrecommend.recommend/2.0/',
    params=params,
    cookies=cookies,
    headers=headers,
)

text = response.text
print(text)